package com.taoyibao.hongcanting.http.interceptor;

import android.content.Context;

import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.utils.LogUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import rx.Observable;
import rx.functions.Action1;


/**
 * 网络请求时的拦截器
 */
public class AddCookiesInterceptor implements Interceptor {
    private static final String TAG = AddCookiesInterceptor.class.getSimpleName();
    private Context context;

    public AddCookiesInterceptor(Context context) {
        super();
        this.context = context;

    }

    @Override
    public Response intercept(Chain chain) throws IOException {

        final Request.Builder builder = chain.request().newBuilder();
        Observable.just(SpUtils.getToken())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String token) {
                        //添加header
                        builder.addHeader("token", token);
                        //content-type
                        // builder.addHeader("content-type","application/json; charset=UTF-8");
                        LogUtils.eNormal(TAG, "token+"+token);
                    }
                });

        Observable.just(SpUtils.getCookie())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String cookie) {
                        builder.addHeader("Cookie", cookie);
//                        Log.d(TAG, "Cookie: "+cookie);
                    }
                });
        return chain.proceed(builder.build());
    }
}